package com.pingtai.user.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import com.pingtai.dao.BaseDao;
import com.pingtai.indent.entity.UserIndent;
import com.pingtai.indent.entity.UserIndentType;
import com.pingtai.security.entity.PubRoles;
import com.pingtai.security.entity.PubUsersRoles;
import com.pingtai.user.entity.User;

public class PubUsersDao extends BaseDao {
	
	public List findUserfanliList(User user){
		Query query = getSession().createQuery("from UserPay  where childUser=:childUser and type=1 and userIndent!=null and type2=null");
		query.setParameter("childUser", user);
		return query.list();
	}
	
	public List findUserKoukuanList(User user){
		Query query = getSession().createQuery("from UserPay  where childUser=:childUser and type=0 and userIndent=null");
		query.setParameter("childUser", user);
		return query.list();
	}
	
	public List findUserChongzhiList(User user){
		Query query = getSession().createQuery("from UserPay  where childUser=:childUser and type=1 and userIndent=null and type2!=4");
		query.setParameter("childUser", user);
		return query.list();
	}

	public List getChildProduceList(User user,UserIndentType indentType){
		Query query = getSession().createQuery("from UserIndentType  where user.userId=? and userBaseIndentType.parentBaseIndentType.id=?");
		query.setParameter(0, user.getUserId());
		query.setParameter(1, indentType.getUserBaseIndentType().getId());
		return query.list();
	}
	
	public List<User> findUserByname(String name){
		Query query = getSession().createQuery("from User  where userAccount=?");
		query.setParameter(0, name);
		return query.list();
	}
	
    public List findRoleByUserName(String name){
    	Query query = getSession().createQuery("from User  where userAccount=? and enabled=1");
    	query.setParameter(0, name);
    	User user =  (User) query.uniqueResult();
    	if(user==null) return null;
    	List<GrantedAuthority> rList = new ArrayList<GrantedAuthority>();
    	List<PubUsersRoles> urList = new ArrayList<PubUsersRoles>(user.getPubUsersRoleses());
    	for (int i = 0; i < urList.size(); i++) {
    		PubUsersRoles ur = urList.get(i);
    		GrantedAuthority ga = new SimpleGrantedAuthority(ur.getPubRoles().getRoleName());
    		rList.add(ga);
		}
    	return rList;
    }
    
    public Double findAllChildUsersMoney(int parentId){
    	Query query = getSession().createQuery("select sum(u.money) from User u where u.parentUserIdString like :parentUserId");
    	query.setParameter("parentUserId", "%"+parentId+"%");
		return (Double) query.list().get(0);
    }
    
    public List findAllChildUsers(int parentId){
    	Query query = getSession().createQuery("from User  where parentUserId=:parentUserId");
		query.setParameter("parentUserId", parentId);
		return query.list();
    }
    
    public PubRoles findRoleByRoleName(String name){
    	Query query = getSession().createQuery("from PubRoles  where roleName=?");
    	query.setParameter(0, name);
    	return (PubRoles) (query.list()==null?null:query.list().get(0));
    }
    
    public List findRebateRelation(UserIndent userIndent){
    	Query query = getSession().createQuery("from IndentRebateRelation  where userIndent=? order by sort asc");
		query.setParameter(0, userIndent);
		return query.list();
    }
    
}
